package com.tarena.lbs.user.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.user.dao.mapper.UserGroupMapper;
import com.tarena.lbs.user.dao.repository.UserGroupRepository;
import com.tarena.lbs.user.dao.repository.UserRepository;
import com.tarena.lbs.user.pojo.po.UserGroupPO;
import com.tarena.lbs.user.pojo.query.UserGroupQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserGroupRepositoryImpl implements UserGroupRepository {

    @Autowired
    private UserGroupMapper userGroupMapper;

    @Override
    public PageInfo<UserGroupPO> pageList(UserGroupQuery query) {
        QueryWrapper<UserGroupPO> queryWrapper = assembleQuery(query);
        // PageHelper分页
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<UserGroupPO> list = userGroupMapper.selectList(queryWrapper);
        return new PageInfo<>(list);
    }



    @Override
    public void save(UserGroupPO userGroupPO) {
        if (userGroupPO.getId() != null) {
            // 更新操作
            userGroupMapper.updateById(userGroupPO);
        } else {
            // 插入操作
            userGroupMapper.insert(userGroupPO);
        }
    }
    private QueryWrapper<UserGroupPO> assembleQuery(UserGroupQuery query) {
        QueryWrapper<UserGroupPO> queryWrapper = new QueryWrapper<>();
        // groupName 人群名称模糊查询
        if (StringUtils.isNotBlank(query.getGroupName())) {
            queryWrapper.like("group_name", query.getGroupName());
        }
        return queryWrapper;
    }
}